package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import entity.PageResult;
import entity.Result;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.engine.internal.NonNullableTransientDependencies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by WF on 2019/9/17 10:26
 */
@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;
    //1.查询所有的标签列表
    public List<Label> findAll() {
        return labelDao.findAll();
    }
    //2.根据标签id查询标签对象
    public Label findOne(String lableId) {
        return labelDao.findById(lableId).get();
    }
    //3.添加标签
    public void add(Label label) {
        //3.1)添加前，先将label的主键设置值
        label.setId(idWorker.nextId()+"");
         labelDao.save(label);
    }
    //4.修改标签
    public void update(Label label) {
        labelDao.save(label);
    }
    //5.删除标签
    public void delete(String id){
        labelDao.deleteById(id);
    }

    //6.条件查询带分页
    public PageResult<Label> search(int page, int size, Label label) {
        //6.1)包装分页参数为对象
        PageRequest pageRequest = PageRequest.of(page-1, size);
        //6.2)进行分页带条件查询
        Page<Label> labelPage = labelDao.findAll(createSpecification(label), pageRequest);
        //6.3)将labelPage对象转换为PageResult对象
        return new PageResult<>(labelPage.getTotalElements(),labelPage.getContent());
    }

    //7.条件查询
    public List<Label> searchByCond(Label label) {
       return labelDao.findAll(createSpecification(label));
    }
    //  select sname,sex,age,addr  from Student
    //7.1)返回查询条件
    private Specification<Label> createSpecification(Label label) {
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //7.1.1)使用数组来封装查询条件
                List<Predicate> predicateList = new ArrayList<>();
                //7.1.2)下面就是封装查询条件的过程
                if(label !=null){
                    if(StringUtils.isNotBlank(label.getLabelname())){
                        //cb.like中的参数1：代表确定要查询参数的名称及类型，参数2：代表参数的具体的值
                        predicateList.add(cb.like(root.get("labelname").as(String.class),"%" + label.getLabelname() + "%"));
                    }
                    if(StringUtils.isNotBlank(label.getState())){
                        predicateList.add(cb.equal(root.get("state").as(String.class),label.getState()));
                    }
                    if(StringUtils.isNotBlank(label.getRecommend())){
                        predicateList.add(cb.equal(root.get("recommend").as(String.class),label.getRecommend()));
                    }
                }
                //7.1.3）定义封装查询条件的数组
                Predicate[] predicates = new Predicate[predicateList.size()];
                //7.1.4)将集合条件转换为数组
                Predicate[] predicatesArr = predicateList.toArray(predicates);
                //7.1.5)将多个连接连接起来
                return cb.and(predicatesArr);
            }
        };
    }
}
